#include "StdAfx.h"
#include "sa_TwSpriteAnimation.h"


#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)



sa_TwSpriteAnimation::sa_TwSpriteAnimation(void)
{
}

sa_TwSpriteAnimation::~sa_TwSpriteAnimation(void)
{
}

void sa_TwSpriteAnimation::InitGraphics()
{
	D3DXCreateSprite(g_Render->GetDevice(), &d3dspt);    // create the Direct3D Sprite object

	D3DXCreateTextureFromFileEx(g_Render->GetDevice(),    // the device pointer
		(LPCSTR)"Panel3.png",    // the file name
		D3DX_DEFAULT,    // default width
		D3DX_DEFAULT,    // default height
		D3DX_DEFAULT,    // no mip mapping
		NULL,    // regular usage
		D3DFMT_A8R8G8B8,    // 32-bit pixels with alpha
		D3DPOOL_MANAGED,    // typical TwMemory handling
		D3DX_DEFAULT,    // no filtering
		D3DX_DEFAULT,    // no mip filtering
		D3DCOLOR_XRGB(255, 0, 255),    // the hot-pink color key
		NULL,    // no image info struct
		NULL,    // not using 256 colors
		&sprite);    // load to sprite


}

void sa_TwSpriteAnimation::InitRenderState()
{
}

void sa_TwSpriteAnimation::InitLight()
{
}

void sa_TwSpriteAnimation::Update()
{
}

void sa_TwSpriteAnimation::Render()
{
	// clear the window to a deep blue
	g_Render->GetDevice()->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0);

	g_Render->GetDevice()->BeginScene();    // begins the 3D scene

	d3dspt->Begin(D3DXSPRITE_ALPHABLEND);    // // begin sprite drawing with transparency

	// DRAW THE SPRITE FRAME

	// count from 0 to 22 to determine the current frame
	static int frame = 21;    // start the program on the final frame
	if(KEY_DOWN(VK_SPACE)) frame=0;     // when the space key is pressed, start at frame 0
	if(frame < 21) frame++;     // if we aren't on the last frame, go to the next frame

	// calculate the x-position
	int xpos = frame * 182 + 1;

	// draw the selected frame using the coordinates
	RECT part;
	SetRect(&part, xpos, 0, xpos + 181, 128);
	D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);    // center at the upper-left corner
	D3DXVECTOR3 position(50.0f, 50.0f, 0.0f);    // position at 50, 50 with no depth
	d3dspt->Draw(sprite, &part, &center, &position, D3DCOLOR_ARGB(127, 255, 255, 255));

	d3dspt->End();    // end sprite drawing

	g_Render->GetDevice()->EndScene();    // ends the 3D scene

	g_Render->GetDevice()->Present(NULL, NULL, NULL, NULL);


}

void sa_TwSpriteAnimation::Clear()
{
	sprite->Release();
}